package com.neusoft.neuiotms.dcam.fixed.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.WarehouseRecordDTO;
import com.neusoft.neuiotms.dcam.fixed.model.WarehouseRecord;
import com.neusoft.neuiotms.dcam.fixed.repository.WarehouseRecordRepository;

@Component
public class WarehouseRecordConverter extends AbstractConverter<WarehouseRecord, WarehouseRecordDTO> {

    @Autowired
    private WarehouseRecordRepository warehouseRecordRepository;

    @Override
    public WarehouseRecord toModel(final WarehouseRecordDTO dto) {

        WarehouseRecord model;
        if (dto.isNew()) {
            model = new WarehouseRecord();
        } else {
            model = this.warehouseRecordRepository.getOne(dto.getId());
        }

        model.setName(dto.getName());
        model.setSpecifications(dto.getSpecifications());
        model.setLocationName(dto.getLocationName());
        model.setLocation(dto.getLocation());
        model.setSmallTypeName(dto.getSmallTypeName());
        model.setSmallTypeCode(dto.getSmallTypeCode());
        model.setContractNo(dto.getContractNo());
        model.setContractName(dto.getContractName());
        model.setBrand(dto.getBrand());
        model.setAmount(dto.getAmount());
        model.setType(dto.getType());

        return model;
    }

    @Override
    public WarehouseRecordDTO toDTO(final WarehouseRecord model, final boolean forListView) {

        final WarehouseRecordDTO dto = new WarehouseRecordDTO();

        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setSpecifications(model.getSpecifications());
        dto.setLocationName(model.getLocationName());
        dto.setLocation(model.getLocation());
        dto.setSmallTypeName(model.getSmallTypeName());
        dto.setSmallTypeCode(model.getSmallTypeCode());
        dto.setContractNo(model.getContractNo());
        dto.setContractName(model.getContractName());
        dto.setBrand(model.getBrand());
        dto.setAmount(model.getAmount());
        dto.setType(model.getType());
        dto.setCreateTime(model.getCreateTime());
        return dto;
    }
}
